package JZOffer;

import java.util.Arrays;

public class _4 {
    class Solution {
        public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
            if (pre.length == 0 || in.length == 0) {
                return null;
            }
            TreeNode node = new TreeNode(pre[0]);
            for (int i = 0; i < in.length; i++) {
                if (pre[0] == in[i]) {
                    node.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, i + 1), Arrays.copyOfRange(in, 0, i));
                    node.right = reConstructBinaryTree(Arrays.copyOfRange(pre, i + 1, pre.length), Arrays.copyOfRange(in, i + 1, in.length));
                }
            }
            return node;
        }
    }

    Solution getSolution() {
        return new Solution();
    }

    public static void main(String args[]) {
        _4 c = new _4();
        _4.Solution s = c.getSolution();
        int[] pre = {10, 6, 4, 8, 14, 12, 16};
        int[] in = {4, 6, 8, 10, 12, 14, 16};
        TreeNode res = s.reConstructBinaryTree(pre, in);

    }
}
